package cn.yougou.serviceImpl;

import cn.yougou.common.CalculateUtil;
import cn.yougou.mapper.YgCommodityMapper;
import cn.yougou.pojo.YgCart;
import cn.yougou.pojo.YgCommodity;
import cn.yougou.pojo.YgCommodityProduct;
import cn.yougou.pojo.YgOrder;
import cn.yougou.service.shopcart.YgCommodityProductService;
import cn.yougou.service.shopcart.YgCommodityService;
import cn.yougou.vo.YgCommodityOrderByVo;
import cn.yougou.vo.YgCommodityVo;
import cn.yougou.vo.YgResCommodityVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import java.math.BigDecimal;
import java.util.*;

/**
 * (YgCommodity)表服务实现类
 *
 * @author 白杰崇
 * @since 2020-12-03 01:32:22
 */
@Service
public class YgCommodityServiceImpl implements YgCommodityService {

    @Autowired
    private YgCommodityMapper ygCommodityMapper;

    @Autowired
    private YgCommodityProductService ygCommodityProductService;

    /**
     * 根据三级类目查询所有商品 & 排序查询商品列表,商品列表展示
     * @param ygCommodityOrderByVo
     * @return
     */
    @Override
    public List<YgCommodityVo> orderBy(YgCommodityOrderByVo ygCommodityOrderByVo) {
        List<YgCommodityVo> voList = new ArrayList<>();
        List<YgCommodity> list = ygCommodityMapper.orderBy(ygCommodityOrderByVo);
        for (YgCommodity comm : list) {
            YgCommodityVo ygCommodityVo = queryById(comm.getId());
            if(ygCommodityVo != null){
                voList.add(ygCommodityVo);
            }
        }
        return voList;
    }

    /**
     * 通过ID查询单条数据，计算折扣价 & 商品详情页面展示
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YgCommodityVo queryById(Integer id) {
        //1 获取到全部数据
        YgCommodity ygCommodity = ygCommodityMapper.queryById(id);
        if(ygCommodity.getCommoditymoney() != null){
            //2 对discount（折扣）做处理，计算出折扣后的价格
            double price = ygCommodity.getCommoditymoney();
            double priceAft = 0.0;
            //不等于-1代表该商品有折扣，则进行计算
            if(ygCommodity.getDiscount() != -1){
                //调用工具类
                priceAft = CalculateUtil.getPrice(price,ygCommodity.getDiscount());
            }
            //3 将原价和折扣价一起放入Vo给到前端
            YgCommodityVo ygCommodityVo = new YgCommodityVo();
            BeanUtils.copyProperties(ygCommodity,ygCommodityVo);
            if(priceAft != 0.0){
                ygCommodityVo.setDiscountAfter(priceAft);
            }
            return ygCommodityVo;
        }
        return null;
    }

    /**
     * 下单成功服务 & 更改商品一系列信息
     *  应该用到事务
     * @return
     */
    @Override
    public boolean placeOrder(List<YgOrder> orderList){
        //判断数组是否为空
        if(!orderList.isEmpty()){
            for (YgOrder order : orderList){
                //1 商品规格表对应的规格库存-x
                boolean config = ygCommodityProductService.placeOrder(
                        order.getCommodityid(),  //商品id
                        order.getProductid(),    //规格组合id
                        order.getPurchseq()      //该规格下单的商品数量
                );
                //true代表规格减库存成功，false代表失败
                if(config){
                    //2 商品表：销量+x & 库存-x                    商品id & 该规格下单的商品数量
                    int configTwo = ygCommodityMapper.placeOrder(order.getCommodityid(),order.getPurchseq());
                }
            }
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询多条数据
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<YgCommodity> queryAllByLimit(int offset, int limit) {
        return this.ygCommodityMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ygCommodity 实例对象
     * @return 实例对象
     */
    @Override
    public YgCommodity insert(YgCommodity ygCommodity) {
        this.ygCommodityMapper.insert(ygCommodity);
        return ygCommodity;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ygCommodityMapper.deleteById(id) > 0;
    }

    /**
     * 查询商品id 和名称
     * */
    public List returnCommodityIDandNameList(){
        List<YgCommodity> list = ygCommodityMapper.orderBy(null);
        List Data = new ArrayList();
        for(int i=0;i<list.size();i++){
            Data.add(new YgCommodity(list.get(i).getId(),list.get(i).getCommodityid()));
        }
        return Data;
    }
/**
 * 通过idList 查询商品List
 * */
    @Override
    public List<YgCommodity> queryAllById(List idList) {
        return ygCommodityMapper.queryAllById(idList);
    }

}